import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/common_widget.dart' show CustomCircleAvatar;
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/im/util/time.dart';
import 'package:find_friend/pages/mine/mine_xspace.dart' show XspaceVoiceView;
import 'package:find_friend/pages/xspace/model/artclemodel.dart';
import 'package:find_friend/pages/xspace/school/view/raise_animated.dart';
import 'package:find_friend/pages/xspace/school/view/xdiscuss_image.dart';
import 'package:flutter/material.dart';

class XdetailDiscussCell2 extends StatefulWidget {

  final ReplyModel model;
  final Function(bool islike) onLikeAction;
  final Function() onRefreshData;

  const XdetailDiscussCell2({Key key, this.model, this.onLikeAction, this.onRefreshData}) : super(key: key);

  @override
  _XdetailDiscussCell2State createState() => _XdetailDiscussCell2State();
}

class _XdetailDiscussCell2State extends State<XdetailDiscussCell2> with SingleTickerProviderStateMixin {

  Animation<double> _animation;
  Animation<double> _scaleAnimation;
  AnimationController _animationController;

  int upvotes = 0;

  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
    CurvedAnimation _cureve = CurvedAnimation(parent: _animationController, curve: Curves.bounceIn);
    _animation = Tween<double>(begin: 0, end: 1).animate(_cureve);
    _scaleAnimation = Tween<double>(end: 0.0, begin: 1.0).animate(_cureve);

    if(_islikeFlag()) _animationController.forward(); 
    if(_isdislikeFlag()) _animationController.reverse(); 
  }

  @override
  void didUpdateWidget(XdetailDiscussCell2 oldWidget) {
    super.didUpdateWidget(oldWidget);
    // 列表刷新的时候会重新走一遍
    if(_islikeFlag()) _animationController.forward(); 
    if(_isdislikeFlag()) _animationController.reverse(); 
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  bool _isdislikeFlag() {
    return widget.model?.dissflag != null;
  }

  bool _islikeFlag() {
    return widget.model?.upvoteflag != null;
  }

  _requestLike(bool islike){
    Networktool.request(API.upvote, params: {"rid": widget.model.id}, method: islike ? NETWORKHTTPMethod.POST : NETWORKHTTPMethod.DELETE, success: (data){
      // animationController.isCompleted ? animationController.reverse() : animationController.forward();
      if(islike){
        upvotes = widget.model.dissflag != null ? 2 : 1;
      } else {
        upvotes = widget.model.upvoteflag != null ? -2 : -1;
      }
      if(islike){
        widget.model.upvoteflag = 1;
        widget.model.dissflag = null;
      } else {
        widget.model.upvoteflag = null;
        widget.model.dissflag = 1;
      }
      setState(() {
        widget.model.upvotes += upvotes;
      });
      islike ? _animationController.forward() : _animationController.reverse();
    }
    ,fail: (msg) => AlertTool.showToast(msg)
    );
  }



  @override
  Widget build(BuildContext context) {

    final size = MediaQuery.of(context).size;

    return Container(
      padding: const EdgeInsets.only(left: 20, right: 20, bottom: 9),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              InkWell(
                onTap: (){
                    Navigator.of(context).pushNamed("/minepreview", arguments: widget.model.mobile);
                },
                child: CustomCircleAvatar(size: 31, url: widget.model.avatar,)
              ),
              SizedBox(width: 8,),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(widget.model.username, style: instanceTextStyle(13, APP_font_black),),
                        Text(TimeUtil.convertTime(widget.model.createtime * 1000), style: instanceTextStyle(9, APP_font_grey),)
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.only(right: 30),
                      child: Text("个性签名：${widget.model.bio}", maxLines: 1, overflow: TextOverflow.ellipsis, style: instanceTextStyle(9, APP_font_grey),),
                    ),

                    SizedBox(height: 5,),
                    // 文字
                    Row(
                      children: [
                        Offstage(
                          offstage: widget.model.callname == null,
                          child: Text.rich(TextSpan(
                            children: [
                              TextSpan(text: "回复",style: instanceTextStyle(14, APP_font_black)),
                              TextSpan(text: (widget.model.callname ?? "") + "：",style: instanceTextStyle(14, APP_main_color)),
                            ]
                          )),
                        ),
                        Offstage(
                          offstage: widget.model.content == null,
                          child: Text(widget.model.content ?? "", style: instanceTextStyle(14, APP_font_black),),
                        ),
                      ],
                    ),

                    // 图片
                    Offstage(
                      offstage: widget.model.photo == null,
                      child: Padding(
                        padding: EdgeInsets.only(top: 9,),
                        child: XDiscussImagesWidget(list: widget.model.photo ?? []),
                      ),
                    ),

                    // 语音
                    Offstage(
                      offstage: widget.model.voice == null,
                      child: Padding(
                        padding: const EdgeInsets.only(top: 9),
                        child: XspaceVoiceView(width: size.width * 0.45, voice: widget.model?.voice,)
                      ),
                    ),
                    // 点赞分享按钮
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Spacer(),
                        InkWell(
                          onTap: () {
                            AlertTool.showBottomShareDialog(context, replyModel: widget.model,onRefreshData: widget.onRefreshData);
                          },
                          child: Image.asset("assets/zhuanfa.png", width: 12,fit: BoxFit.fill,)
                        ),
                        Container(
                        width: 10.w,
                        alignment: Alignment.centerRight,
                        child: Container(width: 0.5, height: 15, color: APP_divider_color,),
                      ),
                        InkWell(
                          onTap: () {
                            _requestLike(true);
                          },
                          child: RiseAnimatedWidget(
                            dissAnimation: _scaleAnimation, 
                            likeAnimation: _animation,
                            imgW: 20, 
                            iszan: true, 
                            value: _islikeFlag(),
                            fit: BoxFit.scaleDown,
                          ),
                        ),
                        Container(
                          width: size.width * 0.1,
                          alignment: Alignment.center,
                          child: Text(widget.model.strUpvotes, style: instanceTextStyle(12, APP_font_black),),
                        ),
                        InkWell(
                          onTap: () {
                            _requestLike(false);
                          },
                          child: RiseAnimatedWidget(
                            dissAnimation: _animation, 
                            likeAnimation: _scaleAnimation,
                            imgW: 20, 
                            iszan: false, 
                            value: _isdislikeFlag(),
                            fit: BoxFit.scaleDown,
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              
            ],
          ),
        ],
      ),
    );
  }
}